import tkinter as tk
from db import db
global t
t = 1


class InputFrame(tk.Frame):  # 继承Frame类
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  # 定义内部变量root
        self.name = tk.StringVar()
        self.math = tk.StringVar()
        self.chinese = tk.StringVar()
        self.english = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        # stick 控件对象方向 tk.W 西方位
        # pady padding y 上下的宽度
        # row 行 表格布局
        tk.Label(self).grid(row=0, stick=tk.W, pady=10)
        tk.Label(self, text='论文名称: ').grid(row=1, stick=tk.W, pady=20)
        # text variable 绑定控件里面的数据内容
        tk.Entry(self, textvariable=self.name).grid(row=1, column=1, stick=tk.E)
        tk.Label(self, text='姓 名: ').grid(row=2, stick=tk.W, pady=10)
        tk.Entry(self, textvariable=self.math).grid(row=2, column=1, stick=tk.E)
        tk.Label(self, text='期刊号: ').grid(row=3, stick=tk.W, pady=10)
        tk.Entry(self, textvariable=self.chinese).grid(row=3, column=1, stick=tk.E)
        tk.Label(self, text='日 期: ').grid(row=4, stick=tk.W, pady=10)
        tk.Entry(self, textvariable=self.english).grid(row=4, column=1, stick=tk.E)
        tk.Button(self, text='录入', command=self.recode_student).grid(row=5, column=1, stick=tk.E, pady=10)
        tk.Label(self, textvariable=self.status).grid(row=6, column=1, stick=tk.E, pady=10)

    def recode_student(self):
        student = {
            'name': self.name.get(),
            'n1': self.math.get(),
            'n2': self.chinese.get(),
            'n3': self.english.get(),
        }
        db.insert(student)
        self.status.set('插入数据成功')
        self._clear_avr()

    def _clear_avr(self):
        self.name.set("")
        self.math.set("")
        self.chinese.set("")
        self.english.set("")


class QueryFrame(tk.Frame):  
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  
        self.itemName = tk.StringVar()

        tk.Label(self, text='查询界面').pack()
        self.table_frame = tk.Frame(self)
        self.table_frame.pack()
        self.row = 1

        self.create_page()

    def create_page(self):
        tk.Button(self, text='刷新数据', command=self.show_data_frame).pack(anchor=tk.E, pady=5)
        self.show_data_frame()

    def show_data_frame(self):
        self._clear_table_frame_label()
        self.row = 1
        students = db.all()
        if students:
            self._line_pact(['论文名称', '姓名', '期刊号', '日期'])
        else:
            self._line_pact(['论文名称', '姓名', '期刊号', '日期'])
            self._line_pact(['', '', '', ''])
        for student in students:
            self._line_pact(student.values())

    def _line_pact(self, line):
        column = 0
        for v in line:
            tk.Label(self.table_frame,
                     text=v,
                     anchor=tk.W,
                     width=10,
                     height=1,
                     bd=8).grid(row=self.row, column=column)
            column += 1
        self.row += 1

    def _clear_table_frame_label(self):
        child = self.table_frame.winfo_children()
        for c in child:
            c.destroy()


class DeleteFrame(tk.Frame):  # 继承Frame类
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  # 定义内部变量root
        tk.Label(self, text='删除数据').pack()
        self.delete_frame = tk.Frame(self)
        self.delete_frame.pack()
        self.status = tk.StringVar()
        self.v1 = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.delete_frame, text='根据论文名称删除信息').pack(anchor=tk.W, padx=20)
        e1 = tk.Entry(self.delete_frame, textvariable=self.v1)
        e1.pack(side=tk.LEFT, padx=20, pady=5)

        tk.Button(self.delete_frame, text='删除', command=self._delete).pack()
        tk.Label(self, textvariable=self.status).pack()

    def _delete(self):
        global t
        if t == 1:
            name = self.v1.get()
            print(name)
            r = db.delete_by_name(name)
            if r:
                self.status.set(f'{name} 已经被删除')
                self.v1.set("")
            else:
                self.status.set(f'{name} 删除失败')
        else:
            name = self.v1.get()
            print(name)
            self.status.set(f'{name} 删除失败')
            t = 1


class ChangeFrame(tk.Frame):  
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  

        tk.Label(self, text='修改界面').pack()
        self.change_frame = tk.Frame(self)
        self.change_frame.pack()
        self.status = tk.StringVar()
        self.name = tk.StringVar()
        self.math = tk.StringVar()
        self.chinese = tk.StringVar()
        self.english = tk.StringVar()

        self.create_page()

    def create_page(self):
        tk.Label(self.change_frame).grid(row=0, stick=tk.W, pady=1)
        tk.Label(self.change_frame, text='论文名称: ').grid(row=1, stick=tk.W, pady=10)
        tk.Entry(self.change_frame, textvariable=self.name).grid(row=1, column=1, stick=tk.E)
        tk.Label(self.change_frame, text='姓 名: ').grid(row=2, stick=tk.W, pady=10)
        tk.Entry(self.change_frame, textvariable=self.math).grid(row=2, column=1, stick=tk.E)
        tk.Label(self.change_frame, text='期刊号: ').grid(row=3, stick=tk.W, pady=10)
        tk.Entry(self.change_frame, textvariable=self.chinese).grid(row=3, column=1, stick=tk.E)
        tk.Label(self.change_frame, text='日期: ').grid(row=4, stick=tk.W, pady=10)
        tk.Entry(self.change_frame, textvariable=self.english).grid(row=4, column=1, stick=tk.E)
        tk.Button(self.change_frame, text='查询', command=self._search).grid(row=6, column=0, stick=tk.W, pady=10)
        tk.Button(self.change_frame, text='修改', command=self._change).grid(row=6, column=1, stick=tk.E, pady=10)
        tk.Label(self.change_frame, textvariable=self.status).grid(row=7, column=1, stick=tk.E, pady=10)

    def _search(self):
        name = self.name.get()
        student = db.search_by_name(name)
        if student:
            self.math.set(student['n1'])
            self.chinese.set(student['n2'])
            self.english.set(student['n3'])
            self.status.set(f'查询到 {name} 论文的信息')
        else:
            self.status.set(f'没有 {name} 论文的信息')

    def _change(self):
        name = self.name.get()
        math = self.math.get()
        chinese = self.chinese.get()
        english = self.english.get()
        stu = {
            'name': name,
            'n1': math,
            'n2': chinese,
            'n3': english,
        }
        r = db.update(stu)
        if r:
            self.status.set(f'{name} 论文的信息更新完毕')
        else:
            self.status.set(f'{name} 论文的信息更新失败')


class AboutFrame(tk.Frame):  
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  

        tk.Label(self, text='管理员界面').pack()
        self.change_frame = tk.Frame(self)
        self.change_frame.pack()
        self.status = tk.StringVar()
        self.name = tk.StringVar()
        self.math = tk.StringVar()
        self.chinese = tk.StringVar()
        self.english = tk.StringVar()

        self.create_page()

    def create_page(self):
        tk.Label(self.change_frame).grid(row=0, stick=tk.W, pady=1)
        tk.Label(self.change_frame, text='论文名称: ').grid(row=1, stick=tk.W, pady=10)
        tk.Entry(self.change_frame, textvariable=self.name).grid(row=1, column=1, stick=tk.E)
        tk.Label(self.change_frame, text='姓 名: ').grid(row=2, stick=tk.W, pady=10)
        tk.Entry(self.change_frame, textvariable=self.math).grid(row=2, column=1, stick=tk.E)
        tk.Label(self.change_frame, text='期刊号: ').grid(row=3, stick=tk.W, pady=10)
        tk.Entry(self.change_frame, textvariable=self.chinese).grid(row=3, column=1, stick=tk.E)
        tk.Label(self.change_frame, text='日期: ').grid(row=4, stick=tk.W, pady=10)
        tk.Entry(self.change_frame, textvariable=self.english).grid(row=4, column=1, stick=tk.E)
        tk.Button(self.change_frame, text='查询', command=self._search).grid(row=6, column=0, stick=tk.W, pady=10)
        tk.Button(self.change_frame, text='审核', command=self._change).grid(row=6, column=1, stick=tk.E, pady=10)
        tk.Label(self.change_frame, textvariable=self.status).grid(row=7, column=1, stick=tk.E, pady=10)

    def _search(self):
        name = self.name.get()
        student = db.search_by_name(name)
        if student:
            self.math.set(student['n1'])
            self.chinese.set(student['n2'])
            self.english.set(student['n3'])
            self.status.set(f'查询到 {name} 论文的信息')
        else:
            self.status.set(f'没有 {name} 论文的信息')

    def _change(self):
        name = self.name.get()
        math = self.math.get()
        chinese = self.chinese.get()
        english = self.english.get()
        stu = {
            'name': name,
            'n1': math,
            'n2': chinese,
            'n3': english,
        }
        r = db.update(stu)
        if r:
            self.status.set(f'{name} 论文审核成功')
            global t
            t = 0
        else:
            self.status.set(f'{name} 论文审核失败')


if __name__ == '__main__':
    t = 1
    root = tk.Tk()
    root.geometry('%dx%d' % (600, 400))
    q = InputFrame(root)
    q.pack()
    root.mainloop()
